<template>
  <el-dialog :title="!dataForm.id ? '新增' : '查看'" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
    <el-form :model="dataForm" :rules="dataRule" label-position="top" ref="dataForm" @keyup.enter.native="dataFormSubmit()">

      <el-form-item label="产品序号" :label-width="formLabelWidth" prop="productId">
        <el-select v-model="dataForm.productId" clearable placeholder="请选择" @change="productModelChange" >
          <el-option v-for="item in productModelOptions" :key="item.id" :label="item.code" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名称" :label-width="formLabelWidth" prop="userCompany">
        <el-input v-model.trim="dataForm.userCompany" placeholder="用户名称(用户公司)" > </el-input>
      </el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth" prop="userName">
        <el-input v-model.trim="dataForm.userName" placeholder="姓名" > </el-input>
      </el-form-item>
      <el-form-item label="电话" :label-width="formLabelWidth" prop="userPhone">
        <el-input v-model.trim="dataForm.userPhone" placeholder="电话" > </el-input>
      </el-form-item>
      <el-form-item label="地址" :label-width="formLabelWidth" prop="userAddress">
        <el-input v-model.trim="dataForm.userAddress" placeholder="地址" > </el-input>
      </el-form-item>
      <el-form-item label="现场问题描述" :label-width="formLabelWidth" prop="problemDesc">
        <el-input v-model.trim="dataForm.problemDesc" type="textarea" > </el-input>
      </el-form-item>
      <el-form-item label="评价" :label-width="formLabelWidth" prop="evaluate">
        <el-radio-group v-model="dataForm.evaluate">
            <el-radio :label="0">不满意</el-radio>
            <el-radio :label="1">满意</el-radio>
      </el-radio-group>
      </el-form-item>
      <el-form-item label="您认为本公司的服务需要改进的地方" :label-width="formLabelWidth" prop="suggestion">
        <el-input v-model.trim="dataForm.suggestion" type="textarea"> </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="dataFormSubmit()" :disabled="isDisabled">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
import { getToken } from "@/utils/auth";
export default {
  emits: ["refreshDataList"],
  data() {
    let testPhone = (rule, value, callback) => {
      let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
      if (value == '') {
        callback();
      } else if (!reg.test(value)) {
        callback(new Error('手机号格式不正确'));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      dialogFormVisible: false,
      isDisabled: false,
      isval: false,
      isupdate: 0,
      istype: "",
      dataForm: {
        id: 0,
        productId: '',
        productName: '',
        userCompany: '',//用户名称（用户公司）
        userName: '',//姓名
        userPhone: '',//电话
        userAddress: '',//地址
        problemDesc: '',//现场问题描述
        suggestion: '',//您认为本公司的服务需要改进的地方
        appendixUrl: '',//附件路径
        appendixName: '',//附件名称
        evaluate:1,
      },
      productModelOptions: [],
      url: process.env.VUE_APP_BASE_API,
      headers: {
        token: getToken() //从cookie里获取token，并赋值  Authorization ，而不是token
      },
      dataRule: {
        userPhone: [
        { validator: testPhone, trigger: 'blur' }
        ],
      },
      formLabelWidth: "70px",
      visible: false,
      handleState:0
      // url: process.env.VUE_APP_BASE_API,
    };
  },
  created() { },
  methods: {
    async init(id) {
      this.dataForm.id = id || 0;
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm.productModel = "";
        this.productModelOptions = []
        this.Sevice({
          url: "/product/list",
          method: "get",
        }).then(async (res) => {
          // console.log(res);
          if (res.code != 200) {
            // this.$message.error(res.data.msg)
          } else {
            this.productModelOptions = res.list
          }
        });
        if (!this.dataForm.id) {
          // 新增
          // this.treeListTreeSetCurrentNode()
        } else {
          //获取详情
          this.Sevice({
            url: "/syn/Feedback/info/" + this.dataForm.id,
            method: "get",
          }).then(async (res) => {
            // console.log(res);
            if (res.code != 200) {
              // this.$message.error(res.data.msg)
            } else {
              this.dataForm.id = res.info.id;
              this.dataForm.problemDesc = res.info.problemDesc;
              this.dataForm.productName = res.info.productName;
              this.dataForm.productId = res.info.productId;
              this.dataForm.suggestion = res.info.suggestion;
              this.dataForm.userAddress = res.info.userAddress;
              this.dataForm.userCompany = res.info.userCompany
              this.dataForm.userName = res.info.userName
              this.dataForm.userPhone = res.info.userPhone
              this.handleState=res.info.handleState
              this.dataForm.evaluate=res.info.evaluate?1:0
            }
          });
        }
      })

    },
    productModelChange(id) {
      for (let i = 0; i < this.productModelOptions.length; i++) {
        if (this.productModelOptions[i].id == id) {
          this.dataForm.productName = this.productModelOptions[i].code
        }
      }

    },
    
    handleSuccess(response, file, fileList) {
      if (response.code == 200) {
        this.fileList = fileList
      } else {
        for (let i = 0; i < fileList.length; i++) {
          if (fileList[i].uid == file.uid) {
            fileList.splice(i, 1);
          }
        }
        this.$message({ message: response.msg, type: 'error', duration: 1000 })
      }
    },
    handleChange(file, fileList) {
      this.fileList = fileList;
    },
    // 表单提交
    async dataFormSubmit() {
      this.$refs["dataForm"].validate(async (valid) => {
        if (valid) {
          let url = this.dataForm.id
            ? "/syn/Feedback/update"
            : "/syn/Feedback/save";
          this.isDisabled = true;
          await this.Sevice({
            url: url,
            method: "post",
            data: {
              id: this.dataForm.id || undefined,
              problemDesc: this.dataForm.problemDesc,
              productId: this.dataForm.productId,
              productName: this.dataForm.productName,
              suggestion: this.dataForm.suggestion,
              userAddress: this.dataForm.userAddress,
              userCompany: this.dataForm.userCompany,
              userName: this.dataForm.userName,
              userPhone: this.dataForm.userPhone,
            },
          }).then((res) => {
            if (res && res.code == 200) {
              this.dialogFormVisible = false;
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.isDisabled = false;
                  this.$emit("getMenuList");
                },
              });
            } else {
              this.isDisabled = false;
            }
          }).catch(error => {
            console.log(error)
            this.isDisabled = false;;
          });
        }
      });
    },
  },
};
</script>

<style scoped>
.mod-menu .menu-list__input>.el-input__inner,
.mod-menu .icon-list__input>.el-input__inner {
  cursor: pointer;
}

.mod-menu__icon-popover {
  width: 458px;
  overflow: hidden;
}

.mod-menu__icon-inner {
  width: 478px;
  max-height: 258px;
  overflow-x: hidden;
  overflow-y: auto;
}

.mod-menu__icon-list {
  width: 458px;
  padding: 0;
  margin: -8px 0 0 -8px;
}

.mod-menu__icon-list>.el-button {
  padding: 8px;
  margin: 8px 0 0 8px;
}

.mod-menu__icon-list>.el-button>span {
  display: inline-block;
  vertical-align: middle;
  width: 18px;
  height: 18px;
  font-size: 18px;
}

.mod-menu .icon-list__tips {
  font-size: 18px;
  text-align: center;
  color: #e6a23c;
  cursor: pointer;
}

.el-tree {
  height: 200px;
  overflow-y: auto;
}
</style>
<style>
.formItem .el-form-item__label{
  float: none;
}
</style>